package com.oms.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1Util {
	
	
	/**
	 * 将源字符串通过MD5进行加密为字节数组
	 * @param source
	 * @return
	 */
	public static byte[] encodeToBytes(String source) {
		byte[] result  = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();//重置
			md.update(source.getBytes("UTF-8"));//添加需要加密的源
			result = md.digest();//加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	/**
	 * 将源字符串通过MD5加密成32位16进制数
	 * @param source
	 * @return
	 */
	public static String encodeToHex(String source) {
		byte[] data = encodeToBytes(source);//先加密为字节数组
		StringBuffer hexSb = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			String hex = Integer.toHexString(0xff & data[i]);
			if (hex.length() == 1) {
				hexSb.append("0");
			}
			hexSb.append(hex);
		}
		return hexSb.toString();
	}

	
	
	/**
	 * 验证字符串是否匹配
	 * @param unknown	待验证的字符串
	 * @param okHex		使用MD5加密后的16进制字符串
	 * @return
	 */
	public static boolean validate(String unknown , String okHex) {
		return okHex.equals(encodeToHex(unknown));
	}
	
	/**
	 * 通过sha1加密字符串
	 * @param str
	 * @return
	 */
	public static String getSha1(String str){
	        if(str==null||str.length()==0){
	            return null;
	        }
	        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
	                'a','b','c','d','e','f'};
	        try {
	            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
	            mdTemp.update(str.getBytes("UTF-8"));

	            byte[] md = mdTemp.digest();
	            int j = md.length;
	            char buf[] = new char[j*2];
	            int k = 0;
	            for (int i = 0; i < j; i++) {
	                byte byte0 = md[i];
	                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
	                buf[k++] = hexDigits[byte0 & 0xf];      
	            }
	            return new String(buf);
	        } catch (Exception e) {
	            // TODO: handle exception
	            return null;
	        }
	    }
	
	
	
	
}
